#coding:utf-8

import web
import urllib
import urllib2


def doPost(url, data):
    data = urllib.urlencode(data)
    request = urllib2.Request(url, data)
    response = urllib2.urlopen(request)
    file = response.read()
    if response.code != 200:
        return 'error code:' + response.code
    else:
        return file


class sinaWbo:
    API_URL="https://api.weibo.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s"
    API_ACCESS_URL="https://api.weibo.com/oauth2/access_token?client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s"
    def __init__(self,api_key,api_secret,redirect_uri):
        self.api_key=api_key
        self.api_secret=api_secret
        self.redirect_uri=urllib.quote(redirect_uri)

    def getCode(self):

        return "<a href='%s'>go</a>"% self.API_URL%(self.api_key,self.redirect_uri)

    def getAccess(self,code):
        url=self.API_ACCESS_URL %(self.api_key,self.api_secret,self.redirect_uri,code)
        #json=doPost(url,{})
        return url
        
        
        

a=sinaWbo('529618010','d39f5d3e6e9eeb7910b5f7fd1187f16e','http://api.jxfuzhou.com/api/')
class autho():


    def GET(self,key):
        data = web.input()

       

        return  a.getAccess(data.code)
        

class index():
    def GET(self, nowpage=1):
        
       
        return  a.getCode()



      
    def POST(self, p):
        up = upload("d:/a/c", 10, ('jpg', 'gif'))
        return up.move(['dd1', 'dd2'])

